<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class #_Class_# extends MY_Controller {
    
    private $model='#_model_#';
	private $table='#_table_#';

	function __construct()
    {
        parent::__construct();
		$this->check_manage_session();//判断用户是否登录
    }
	
	public function index()
	{
		$this->page();
	}
	
	//列表方法
	public function page()
	{
		
		$page=15;
		$return=$this->mypage($this->model,$this->table,$page);
	    $channel_id = $this->admin_model->get_field("channel","id","en_name='$this->model'");
		$data['channel'] = $this->admin_model->get_show("channel",$channel_id);
		$data['list'] = $this->admin_model->get_list($this->table,$return['pagesize'], $return['offset']);
		$data['sidelist'] = $this->admin_model->get_list("channel","","","","sort ASC,id DESC");
		$this->load->view($this -> config->item('system_manage_path').$this->model.'_list',$data);
	}
		
	public function add()
	{
	    $channel_id = $this->admin_model->get_field("channel","id","en_name='$this->model'");
	    $data['channel'] = $this->admin_model->get_show("channel",$channel_id);
	    $data['channel_id'] = $channel_id;
	    $data['show'] = $this->admin_model->get_select_max($this->table);
	    $data['catlist'] = $this->admin_model->get_list('category',"","","channel_id='$channel_id'","root_id ASC,sort ASC,id ASC");
		$data['sidelist'] = $this->admin_model->get_list("channel","","","","sort ASC,id DESC");
	    $this->load->view($this -> config->item('system_manage_path').$this->model.'_add',$data);
	}
	
	public function edit($id)
	{
	    $channel_id = $this->admin_model->get_field("channel","id","en_name='$this->model'");
	    $data['channel'] = $this->admin_model->get_show("channel",$channel_id);
	    $data['channel_id'] = $channel_id;
	    $data['show'] = $this->admin_model->get_show($this->table,$id);
		$data['catlist'] = $this->admin_model->get_list('category',"","","channel_id='$channel_id'","root_id ASC,sort ASC,id ASC");
		$data['sidelist'] = $this->admin_model->get_list("channel","","","","sort ASC,id DESC");
		$this->load->view($this -> config->item('system_manage_path').$this->model.'_edit',$data);
	}
	
	public function save($action,$id=NULL){
		$post=$this->input->post();
		$data=unset_array($post,'lcomeurl'); //过滤无效表单项
		
		$lcomeurl=get_comurl($this->input->post('lcomeurl'));
		
		if($action=='add' or $action=='edit'){
		    if(empty($post['title'])){
		        $this->error('标题不能为空',2);
		    }
		    if(empty($post['sort'])){
		        $this->error('排序不能为空',2);
		    }
		}
		
		if($action=='add'){		
		    $action_text="添加";
		    $data['createtime'] = time();
			$result=$this->admin_model->add($this->table,$data);
		}elseif($action=='edit'){
		    $action_text="修改";
		    if(empty($id)){
		        $this->error('参数错误',2);
		    }
			$result=$this->admin_model->edit($this->table,$data,$id);
		}elseif($action=='del'){
		    $action_text="删除";
			if(empty($id)){
				$this->error('参数错误',2);
			}
			$result=$this->admin_model->del($this->table,$id);
		}
		
		if($result){
		    $lcomeurl=str_replace('welcome', $this->model, $lcomeurl);
			$this->success($action_text.'成功',2,$lcomeurl);
		}else{
			$this->error($action_text.'失败',2);
		}
	}
	
}
